#第一题
import time
import IPy
from pythonping import ping
from multiprocessing import Process


# ping 网段地址
def test_ip(ip):
    res = ping(ip, timeout=1)
    time.sleep(2)
    with open("ip_check.txt", 'a+') as fp:
        if res.success():
            print(str(ip) + ":" + "up")
            fp.write(str(ip) + ":" + "up\n")
        else:
            print(str(ip) + ":" + "down")
            fp.write(str(ip) + ":" + "down\n")


if __name__ == '__main__':
    # ip_net=input("输入网段:")
    ip_net = "192.168.255.0/24"
    ips = IPy.IP(ip_net)
    p_lst = []
    for ip in ips:
        p = Process(target=test_ip, args=(str(ip),))
        p_lst.append(p)
        p.start()
    #列表推导式
    [p.join() for p in p_lst]
    print("end...")


#第二题
import re
import requests


def get_img_url(url):
    request = requests.get(url)
    img_url = re.findall(r"(?<=img src=)\S+.[png||jpg]", request.text)
    for i in img_url:
        img_request = requests.get(url+i)
        save_file(i, img_request.content)


def save_file(filename, content):
    with open(filename, "wb") as fp:
        fp.write(content)


if __name__ == "__main__":
    url = "https://www.sanchuangedu.cn/"
    get_img_url(url)